package com.imedway.ihe.dd.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.imedway.ihe.dd.vo.*;
import org.openehealth.ipf.commons.ihe.xds.core.metadata.*;
import org.openehealth.ipf.commons.ihe.xds.core.requests.QueryRegistry;
import org.openehealth.ipf.commons.ihe.xds.core.requests.query.*;

import java.util.Arrays;
import java.util.Collections;

/**
 * @author zhongyj <1126834403@qq.com><br/>
 * @date 2024/7/23
 */
public class QueryRegistryUtil {

    public static QueryRegistry buildFindDocumentsQueryRegistry(Iti18QueryVO<FindDocumentsVO> findDocumentsVO) {
        FindDocumentsQuery documentsQuery = new FindDocumentsQuery();
        if (StrUtil.isNotBlank(findDocumentsVO.getParams().get$XDSDocumentEntryPatientId())) {
            documentsQuery.setPatientId(new Identifiable(findDocumentsVO.getParams().get$XDSDocumentEntryPatientId()));
        }
        if (StrUtil.isNotBlank(findDocumentsVO.getParams().get$XDSDocumentEntryStatus())) {
            documentsQuery.setStatus(CollUtil.newArrayList(AvailabilityStatus.valueOfOpcode(findDocumentsVO.getParams().get$XDSDocumentEntryStatus())));
        }
        if (StrUtil.isNotBlank(findDocumentsVO.getParams().get$XDSDocumentEntryCreationTimeFrom())) {
            documentsQuery.getCreationTime().setFrom(findDocumentsVO.getParams().get$XDSDocumentEntryCreationTimeFrom());
        }
        if (StrUtil.isNotBlank(findDocumentsVO.getParams().get$XDSDocumentEntryCreationTimeTo())) {
            documentsQuery.getCreationTime().setTo(findDocumentsVO.getParams().get$XDSDocumentEntryCreationTimeTo());
        }
        if (StrUtil.isNotBlank(findDocumentsVO.getParams().get$XDSDocumentEntryServiceStartTimeFrom())) {
            documentsQuery.getServiceStartTime().setFrom(findDocumentsVO.getParams().get$XDSDocumentEntryServiceStartTimeFrom());
        }
        if (StrUtil.isNotBlank(findDocumentsVO.getParams().get$XDSDocumentEntryServiceStartTimeTo())) {
            documentsQuery.getServiceStartTime().setTo(findDocumentsVO.getParams().get$XDSDocumentEntryServiceStartTimeTo());
        }
        if (StrUtil.isNotBlank(findDocumentsVO.getParams().get$XDSDocumentEntryHealthcareFacilityTypeCode())) {
            documentsQuery.setHealthcareFacilityTypeCodes(CollUtil.newArrayList(new Code(findDocumentsVO.getParams().get$XDSDocumentEntryHealthcareFacilityTypeCode(), null, null)));
        }
        if (StrUtil.isNotBlank(findDocumentsVO.getParams().get$XDSDocumentEntryAuthorPerson())) {
            documentsQuery.setAuthorPersons(CollUtil.newArrayList(findDocumentsVO.getParams().get$XDSDocumentEntryAuthorPerson()));
        }
        if (StrUtil.isNotBlank(findDocumentsVO.getParams().get$XDSDocumentEntryTypeCode())) {
            documentsQuery.setTypeCodes(CollUtil.newArrayList(new Code(findDocumentsVO.getParams().get$XDSDocumentEntryTypeCode(), null, null)));
        }
        if (StrUtil.isNotBlank(findDocumentsVO.getParams().get$XDSDocumentEntryClassCode())) {
            documentsQuery.setClassCodes(CollUtil.newArrayList(new Code(findDocumentsVO.getParams().get$XDSDocumentEntryClassCode(), null, null)));
        }
        return new QueryRegistry(documentsQuery);
    }

    public static QueryRegistry buildFindSubmissionSetsQueryRegistry(Iti18QueryVO<FindSubmissionSetsVO> findSubmissionSetsVO) {
        FindSubmissionSetsQuery submissionSetsQuery = new FindSubmissionSetsQuery();
        if (StrUtil.isNotBlank(findSubmissionSetsVO.getParams().get$XDSSubmissionSetPatientId())) {
            submissionSetsQuery.setPatientId(new Identifiable(findSubmissionSetsVO.getParams().get$XDSSubmissionSetPatientId()));
        }
        if (StrUtil.isNotBlank(findSubmissionSetsVO.getParams().get$XDSSubmissionSetSourceId())) {
            submissionSetsQuery.setSourceIds(CollUtil.newArrayList(findSubmissionSetsVO.getParams().get$XDSSubmissionSetSourceId()));
        }
        if (StrUtil.isNotBlank(findSubmissionSetsVO.getParams().get$XDSSubmissionSetSubmissionTimeFrom())) {
            submissionSetsQuery.getSubmissionTime().setFrom(findSubmissionSetsVO.getParams().get$XDSSubmissionSetSubmissionTimeFrom());
        }
        if (StrUtil.isNotBlank(findSubmissionSetsVO.getParams().get$XDSSubmissionSetSubmissionTimeTo())) {
            submissionSetsQuery.getSubmissionTime().setTo(findSubmissionSetsVO.getParams().get$XDSSubmissionSetSubmissionTimeTo());
        }
        if (StrUtil.isNotBlank(findSubmissionSetsVO.getParams().get$XDSSubmissionSetStatus())) {
            submissionSetsQuery.setStatus(CollUtil.newArrayList(AvailabilityStatus.valueOfOpcode(findSubmissionSetsVO.getParams().get$XDSSubmissionSetSourceId())));
        }
        if (StrUtil.isNotBlank(findSubmissionSetsVO.getParams().get$XDSSubmissionSetAuthorPerson())) {
            submissionSetsQuery.setAuthorPerson(findSubmissionSetsVO.getParams().get$XDSSubmissionSetAuthorPerson());
        }
        if (StrUtil.isNotBlank(findSubmissionSetsVO.getParams().get$XDSSubmissionSetContentType())) {
            submissionSetsQuery.setContentTypeCodes(CollUtil.newArrayList(new Code(findSubmissionSetsVO.getParams().get$XDSSubmissionSetContentType(), null, null)));
        }
        return new QueryRegistry(submissionSetsQuery);
    }

    public static QueryRegistry buildFindFoldersQueryRegistry(Iti18QueryVO<FindFoldersVO> findFoldersVO) {
        FindFoldersQuery findFoldersQuery = new FindFoldersQuery();
        if (StrUtil.isNotBlank(findFoldersVO.getParams().get$XDSFolderPatientId())) {
            findFoldersQuery.setPatientId(new Identifiable(findFoldersVO.getParams().get$XDSFolderPatientId()));
        }
        if (StrUtil.isNotBlank(findFoldersVO.getParams().get$XDSFolderLastUpdateTimeFrom())) {
            findFoldersQuery.getLastUpdateTime().setFrom(findFoldersVO.getParams().get$XDSFolderLastUpdateTimeFrom());
        }
        if (StrUtil.isNotBlank(findFoldersVO.getParams().get$XDSFolderLastUpdateTimeTo())) {
            findFoldersQuery.getLastUpdateTime().setTo(findFoldersVO.getParams().get$XDSFolderLastUpdateTimeTo());
        }
        if (StrUtil.isNotBlank(findFoldersVO.getParams().get$XDSFolderStatus())) {
            findFoldersQuery.setStatus(CollUtil.newArrayList(AvailabilityStatus.valueOfOpcode(findFoldersVO.getParams().get$XDSFolderStatus())));
        }
        if (StrUtil.isNotBlank(findFoldersVO.getParams().get$XDSFolderCodeList())) {
            QueryList<Code> codes = new QueryList<>();
            codes.getOuterList().add(CollUtil.newArrayList(new Code(findFoldersVO.getParams().get$XDSFolderCodeList(), (LocalizedString) null, null)));
            findFoldersQuery.setCodes(codes);
        }
        return new QueryRegistry(findFoldersQuery);
    }

    public static QueryRegistry buildGetAssociationsQueryRegistry(Iti18QueryVO<GetAssociationsVO> getAssociationsVO) {
        GetAssociationsQuery query = new GetAssociationsQuery();
        query.setHomeCommunityId(getAssociationsVO.getParams().get$homeCommunityId());
        query.setUuids(CollUtil.newArrayList(getAssociationsVO.getParams().get$uuid()));
        return new QueryRegistry(query);
    }

    public static QueryRegistry buildGetDocumentsAndAssociationsQueryRegistry(Iti18QueryVO<GetDocumentsAndAssociationsVO> getDocumentsAndAssociationsVO) {
        GetDocumentsAndAssociationsQuery query = new GetDocumentsAndAssociationsQuery();
        query.setHomeCommunityId(getDocumentsAndAssociationsVO.getParams().get$homeCommunityId());
        query.setUuids(CollUtil.newArrayList(getDocumentsAndAssociationsVO.getParams().get$XDSDocumentEntryEntryUUID()));
        query.setUniqueIds(CollUtil.newArrayList(getDocumentsAndAssociationsVO.getParams().get$XDSDocumentEntryUniqueId()));
        return new QueryRegistry(query);
    }

    public static QueryRegistry buildGetSubmissionSetsQueryRegistry(Iti18QueryVO<GetSubmissionSetsVO> getSubmissionSetsVO) {
        GetSubmissionSetsQuery query = new GetSubmissionSetsQuery();
        query.setHomeCommunityId(getSubmissionSetsVO.getParams().get$homeCommunityId());
        query.setUuids(CollUtil.newArrayList(getSubmissionSetsVO.getParams().get$uuid()));
        return new QueryRegistry(query);
    }

    public static QueryRegistry buildGetFolderAndContentsQueryRegistry(Iti18QueryVO<GetFolderAndContentsVO> getFolderAndContentsVO) {
        GetFolderAndContentsQuery query = new GetFolderAndContentsQuery();
        query.setUuid(getFolderAndContentsVO.getParams().get$XDSFolderEntryUUID());
        query.setUniqueId(getFolderAndContentsVO.getParams().get$XDSFolderUniqueId());
        query.setFormatCodes(CollUtil.newArrayList(new Code(getFolderAndContentsVO.getParams().get$XDSDocumentEntryFormatCode(), (LocalizedString) null, null)));
        QueryList<Code> confidentialityCodes = new QueryList<>();
        confidentialityCodes.getOuterList().add(CollUtil.newArrayList(new Code(getFolderAndContentsVO.getParams().get$XDSDocumentEntryConfidentialityCode(), (LocalizedString) null, null)));
        query.setConfidentialityCodes(confidentialityCodes);
        query.setHomeCommunityId(getFolderAndContentsVO.getParams().get$homeCommunityId());
        query.setDocumentEntryTypes(CollUtil.newArrayList(DocumentEntryType.valueOfUuid(getFolderAndContentsVO.getParams().get$XDSDocumentEntryType())));
        return new QueryRegistry(query);
    }

    public static QueryRegistry buildGetFoldersForDocumentQueryRegistry(Iti18QueryVO<GetFoldersForDocumentVO> getFoldersForDocumentVO) {
        GetFoldersForDocumentQuery query = new GetFoldersForDocumentQuery();
        query.setHomeCommunityId(getFoldersForDocumentVO.getParams().get$homeCommunityId());
        query.setUuid(getFoldersForDocumentVO.getParams().get$XDSDocumentEntryEntryUUID());
        query.setUniqueId(getFoldersForDocumentVO.getParams().get$XDSDocumentEntryUniqueId());
        return new QueryRegistry(query);
    }

    public static QueryRegistry buildGetAllQueryRegistry(Iti18QueryVO<GetAllVO> getAllVO) {
        GetAllQuery query = new GetAllQuery();
        query.setPatientId(new Identifiable(getAllVO.getParams().get$patientId()));
        query.setStatusDocuments(CollUtil.newArrayList(AvailabilityStatus.valueOfOpcode(getAllVO.getParams().get$XDSDocumentEntryStatus())));
        query.setStatusSubmissionSets(CollUtil.newArrayList(AvailabilityStatus.valueOfOpcode(getAllVO.getParams().get$XDSSubmissionSetStatus())));
        query.setStatusFolders(CollUtil.newArrayList(AvailabilityStatus.valueOfOpcode(getAllVO.getParams().get$XDSFolderStatus())));
        query.setFormatCodes(CollUtil.newArrayList(new Code(getAllVO.getParams().get$XDSDocumentEntryFormatCode(), (LocalizedString) null, null)));
        QueryList<Code> codes = new QueryList<>();
        codes.getOuterList().add(CollUtil.newArrayList(new Code(getAllVO.getParams().get$XDSDocumentEntryConfidentialityCode(), (LocalizedString) null, null)));
        query.setConfidentialityCodes(codes);
        query.setDocumentEntryTypes(CollUtil.newArrayList(DocumentEntryType.valueOfUuid(getAllVO.getParams().get$XDSDocumentEntryType())));
        return new QueryRegistry(query);
    }
}
